package com.javabasic.algorithm.newcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * @Author xiongmin
 * @Description
 * @Date 2021-04-01 19:42
 * @Version 1.0
 **/

/**
 * 废弃价格定义：
 * {1-[(权益包包含目标权益的个数)/(权益包的总个数)]}*price
 * 将权益包排序：按照 1. 废弃价格 从小到大 2. 权益包均值最小
 * 每次使用权益包均要刷新废弃价格
 */
public class 携程商旅 {

    private static Scanner cin = new Scanner(System.in);


    public static void main(String[] args) {
        int main = main();
        System.out.println(main);
    }

    public static int main() {
        String line1 = cin.nextLine();
        String line2 = cin.nextLine();
        String line3 = cin.nextLine();
        String line4 = cin.nextLine();

        int n = Integer.parseInt(line1.trim());


        HashSet<Integer> valueSet = new HashSet<>();
        String[] split4 = line4.trim().split(" ");
        int[] valueNum = new int[split4.length];
        int tag = 0;
        int v;
        for (String s : split4) {
            v = Integer.parseInt(s);
            valueNum[tag++] = v;
            valueSet.add(v);
        }


        String[] split3 = line3.trim().split(" ");
//        int[] price = new int[split3.length];
        ArrayList<Integer> price = new ArrayList<>();
        TreeMap<Integer, Integer> indexToPrice = new TreeMap<>();
        tag = 0;
        for (String s : split3) {
            v = Integer.parseInt(s);
            indexToPrice.put(v, tag);
            price.add(v);
            tag++;
        }

        double[] priceAnthor = new double[split3.length];


        String[] packageStr = line2.trim().split(" ");
        ArrayList<Object> packageList = new ArrayList<>();
        tag = 0;

        HashMap<Integer, ArrayList<Integer>> priceToPackage = new HashMap<>();
        boolean flag = false;
        for (String pac : packageStr) {
            String[] split = pac.trim().split(",");
            ArrayList<Integer> integers = new ArrayList<>();
            flag = false;
            for (String value : split) {
                v = Integer.parseInt(value);
                integers.add(v);
                if (valueSet.contains(v)) {
                    valueSet.remove(v);
                }
                if (price.contains(v)) {
                    flag = true;
                }
            }
            packageList.add(integers);

            if (flag) {
                priceToPackage.put(tag, integers);
            }
            tag++;
        }

        if (!valueSet.isEmpty()) {
            return -1;
        }

        for (int va : valueNum) {
            valueSet.add(va);
        }

        Integer index;
        int result = 0;
        for (Integer priceKey : indexToPrice.keySet()) {
            index = indexToPrice.get(priceKey);
            if (priceToPackage.containsKey(index)) {
                ArrayList<Integer> integers = priceToPackage.get(index);
                for (Integer integer : integers) {
                    valueSet.remove(integer);
                }
                result += priceKey;
                if (valueSet.isEmpty()) {
                    break;
                }
            }
        }
        return result;


    }
}
